﻿using DataType.CustomEnumType;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataType.ShootJudgeTables
{
    public class IndirectedShootJudgeTable
    {
        private int[,] noAlignJMFireDispersalTable;
        private int[,] hexAlignJMFireDispersalTable;
        private int[] targetAlignJMFireDispersalTable;
        private int[,] hitTargetDamageTab;
        private int[,] deviateTargetDamageTab;

        public IndirectedShootJudgeTable(string path)
        {
            string judgeTablePath = path + @"\indirected_judge_tables\";

            // 无校射间瞄火力散布裁决表
            string noAlignTableString = File.ReadAllText(Path.Combine(judgeTablePath, "no_align_table.json"));
            noAlignJMFireDispersalTable = JsonConvert.DeserializeObject<int[,]>(noAlignTableString);

            // 格内校射间瞄火力散布裁决表
            string hexAlignJMFireDispersalTableString = File.ReadAllText(Path.Combine(judgeTablePath, "hex_align_table.json"));
            hexAlignJMFireDispersalTable = JsonConvert.DeserializeObject<int[,]>(hexAlignJMFireDispersalTableString);

            // 目标校射间瞄火力散布裁决表
            string targetAlignJMFireDispersalTableString = File.ReadAllText(Path.Combine(judgeTablePath, "target_align_taable.json"));
            targetAlignJMFireDispersalTable = JsonConvert.DeserializeObject<int[]>(targetAlignJMFireDispersalTableString);

            // 间瞄火力命中目标战斗结果
            string hitTargetDamageTabString = File.ReadAllText(Path.Combine(judgeTablePath, "hit_target_damage_tab.json"));
            hitTargetDamageTab = JsonConvert.DeserializeObject<int[,]>(hitTargetDamageTabString);

            // 间瞄火力偏离目标战斗结果
            string deviateTargetDamageTabString = File.ReadAllText(Path.Combine(judgeTablePath, "deviate_target_damage_tab.json"));
            deviateTargetDamageTab = JsonConvert.DeserializeObject<int[,]>(deviateTargetDamageTabString);
        }

        public int GetNoAlignJMFireDispersal(int shootDistance, int random)
        {
            int col = -1;
            if (shootDistance <= 20)
            {
                col = 0;
            }
            else if (shootDistance > 20 && shootDistance <= 40)
            {
                col = 1;
            }
            else if (shootDistance > 40 && shootDistance <= 60)
            {
                col = 2;
            }
            else if (shootDistance > 60)
            {
                col = 3;
            }

            if (random < 2) random = 2;
            if (random > 12) random = 12;

            return noAlignJMFireDispersalTable[random - 2, col];
        }
        public int GetHexAlignJMFireDispersal(int shootDistance, int random)
        {
            int col = -1;
            if (shootDistance <= 20)
            {
                col = 0;
            }
            else if (shootDistance > 20 && shootDistance <= 40)
            {
                col = 1;
            }
            else if (shootDistance > 40 && shootDistance <= 60)
            {
                col = 2;
            }
            else if (shootDistance > 60)
            {
                col = 3;
            }

            if (random < 2) random = 2;
            if (random > 12) random = 12;

            return hexAlignJMFireDispersalTable[random - 2, col];
        }
        public int GetTargetAlignJMFireDispersal(int random)
        {
            if (random < 2) random = 2;
            if (random > 12) random = 12;

            return targetAlignJMFireDispersalTable[random - 2];
        }
        public int GetHitTargetDamage(WeaponType weaponType, int random)
        {
            int col = -1;
            if (weaponType == WeaponType.LightGun)
            {
                col = 0;
            }
            else if (weaponType == WeaponType.MediumGun)
            {
                col = 1;
            }
            else if (weaponType == WeaponType.HeavyGun)
            {
                col = 2;
            }

            if (random < 2) random = 2;
            if (random > 12) random = 12;

            return hitTargetDamageTab[random - 2, col];
        }
        public int GetDeviateTargetDamage(WeaponType weaponType, int random)
        {
            int col = -1;
            if (weaponType == WeaponType.LightGun)
            {
                col = 0;
            }
            else if (weaponType == WeaponType.MediumGun)
            {
                col = 1;
            }
            else if (weaponType == WeaponType.HeavyGun)
            {
                col = 2;
            }

            if (random < 2) random = 2;
            if (random > 12) random = 12;

            return deviateTargetDamageTab[random - 2, col];
        }
    }
}
